package org.lds.hotkey.client.autoconfigure.hotkey.report;

import lombok.extern.slf4j.Slf4j;
import org.lds.hotkey.common.model.ReportRuleModel;
import org.lds.hotkey.common.model.dto.ResultDTO;
import org.lds.hotkey.worker.remote.model.ReceiveRuleModelRequest;
import org.lds.hotkey.worker.remote.util.WorkerClient;

@Slf4j
public class WorkerTask implements Runnable {
    private String address;
    private ReportRuleModel reportRuleModel;

    public WorkerTask(String address, ReportRuleModel reportRuleModel) {
        this.address = address;
        this.reportRuleModel = reportRuleModel;
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();

        ResultDTO resultDTO = WorkerClient.receiveRuleModel(address, buildReceiveRuleModelRequest());
        long elapsedTime = System.currentTimeMillis() - start;

        if (!resultDTO.isSuccess()) {
            log.warn("key推送失败 address={} data={} 耗时{}ms", address, reportRuleModel, elapsedTime);
        } else {
            log.info("key推送成功 address={} data={} 耗时{}ms", address, reportRuleModel, elapsedTime);
        }
    }

    private ReceiveRuleModelRequest buildReceiveRuleModelRequest() {
        return ReceiveRuleModelRequest.builder().reportRuleModel(reportRuleModel).build();
    }
}
